package com.zuo_book.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zuo_book.common.Result;
import com.zuo_book.common.SecurityUtils;
import com.zuo_book.dto.SysProjectDTO;
import com.zuo_book.entity.SysProject;
import com.zuo_book.entity.SysProjectBo;
import com.zuo_book.service.SysProjectMemberService;
import com.zuo_book.service.SysProjectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 项目管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/project")
@RequiredArgsConstructor
public class SysProjectController {

    private final SysProjectService projectService;
    private final SysProjectMemberService projectMemberService;

    /**
     * 创建项目
     */
    @PostMapping("/create")
    public Result<Long> createProject(@RequestBody SysProjectBo project) {
        // 设置创建者为当前用户
        Long currentUserId = SecurityUtils.getCurrentUserId();
        project.setCreateBy(currentUserId);
        
        // 初始化项目状态和进度
        project.setStatus(0); // 进行中
        project.setProgress(0); // 初始进度为0
        
        Long projectId = projectService.createProject(project);
        return Result.success(projectId);
    }

    /**
     * 更新项目
     */
    @PutMapping("/update")
    public Result<Void> updateProject(@RequestBody SysProject project) {
        // 检查当前用户是否有权限更新项目
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(project.getId(), currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能更新项目");
        }
        
        projectService.updateProject(project);
        return Result.success();
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/info/{projectId}")
    public Result<SysProjectDTO> getProjectInfo(@PathVariable Long projectId) {
        // 检查当前用户是否是项目成员
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (!projectMemberService.isProjectMember(projectId, currentUserId)) {
            return Result.fail("您不是该项目的成员");
        }
        
        SysProjectDTO project = projectService.getProjectById(projectId);
        return Result.success(project);
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/delete/{projectId}")
    public Result<Void> deleteProject(@PathVariable Long projectId) {
        // 检查当前用户是否有权限删除项目
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(projectId, currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能删除项目");
        }
        
        projectService.deleteProject(projectId);
        return Result.success();
    }

    /**
     * 分页查询项目列表
     */
    @GetMapping("/page")
    public Result<Page<SysProjectDTO>> pageProjects(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword) {
        Page<SysProjectDTO> projectPage = projectService.listProjects(page, size, keyword);
        return Result.success(projectPage);
    }

    /**
     * 更新项目状态
     */
    @PutMapping("/status/{projectId}")
    public Result<Void> updateProjectStatus(
            @PathVariable Long projectId,
            @RequestParam Integer status) {
        // 检查当前用户是否有权限更新项目状态
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(projectId, currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能更新项目状态");
        }
        
        projectService.updateProjectStatus(projectId, status);
        return Result.success();
    }

    /**
     * 更新项目进度
     */
    @PutMapping("/progress/{projectId}")
    public Result<Void> updateProjectProgress(
            @PathVariable Long projectId,
            @RequestParam Integer progress) {
        // 检查当前用户是否有权限更新项目进度
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(projectId, currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能更新项目进度");
        }
        
        projectService.updateProjectProgress(projectId, progress);
        return Result.success();
    }

    /**
     * 获取用户参与的项目列表
     */
    @GetMapping("/user")
    public Result<Page<SysProjectDTO>> getUserProjects(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Page<SysProjectDTO> projectPage = projectService.getUserProjects(currentUserId, page, size);
        return Result.success(projectPage);
    }

    /**
     * 获取用户创建的项目
     */
    @GetMapping("/user/create")
    public Result<Page<SysProject>> getUserCreatedProjects(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Page<SysProject> projectPage = projectService.getUserCreatedProjects(currentUserId, page, size);
        return Result.success(projectPage);
    }
}